//
//  MyFocusViewcontroller.m
//  安好
//
//  Created by 马晓良 on 14-12-2.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "MyFocusViewcontroller.h"
#import "MyFocusMainCell.h"
#import "MyFocusObjc.h"
#import "IllnessViewController.h"
#import "HospitalViewController.h"
#import "CirclePostsDetailsViewController.h"
#import "DrugViewController.h"
#import "MyNewsViewController.h"
#import "DoctorViewController.h"
#import "EmergencyViewController.h"
#import "DrugAttentionViewController.h"
@interface MyFocusViewcontroller ()<UITableViewDataSource,UITableViewDelegate>
/**
 *  数据数组
 */
@property (strong, nonatomic) IBOutlet UITableView *myFoucsTableView;
/**
 *  存放类型（话题）
 */
@property (nonatomic, strong) NSArray *typeArr;
/**
 *  存放网络请求数据
 */
@property (nonatomic, strong) NSMutableArray *myFocusData;
@end

@implementation MyFocusViewcontroller


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNaviBarAttribute];
    self.myFoucsTableView.dataSource = self;
    self.myFoucsTableView.delegate = self;
    self.navigationItem.title = @"我的收藏";
    self.typeArr = @[@"话题",@"订阅内容",@"疾病",@"医院",@"药品",@"急症",@"医生"];
    self.myFoucsTableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.myFocusData removeAllObjects];
    [self _parsePersonalDataSource:kBaseURL];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 设置naviBar 并实现左右naviBar button方法 -
- (void)setNaviBarAttribute{
    /**
     *  设置左NaviBar和右naviBar
     */
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 23, 23);
    [leftButton setImage:[UIImage imageNamed:@"公用_返回"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(handleLeftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
}


/**
 *  返回主页面
 */
- (void)handleLeftButtonAction{
    [self.navigationController popToRootViewControllerAnimated:YES];
}



#pragma mark - tableView的代理方法 -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.myFocusData.count > 0) {
        if (section == 0) {
            return 2;
        }else{
            return 5;
        }
    }else{
        return 0;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (kScreenWidth == 414) {
        return 54;
    }else{
        return 44;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.5;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyFocusMainCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myFocusMainCell"];
    if (!cell) {
        if (kScreenWidth == 414) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"MyFocusMainCell" owner:self options:nil] lastObject];
        }else{
            cell = [[[NSBundle mainBundle] loadNibNamed:@"MyFocusMainCell" owner:self options:nil] firstObject];
        }
    }
    if (indexPath.section == 0) {
        cell.focusTypeLabel.text = self.typeArr[indexPath.row];
        if (indexPath.row == 0) {
            cell.focusContentLabel.text = [NSString stringWithFormat:@"%@个",self.myFocusData[0]];
        }else{
            cell.focusContentLabel.text = [NSString stringWithFormat:@"%@个",self.myFocusData[5]];
        }
    }else{
        cell.focusTypeLabel.text = self.typeArr[indexPath.row + 2];
        if (indexPath.row == 0) {
            cell.focusContentLabel.text = [NSString stringWithFormat:@"%@个",self.myFocusData[1]];
        }else if (indexPath.row == 1){
            cell.focusContentLabel.text = [NSString stringWithFormat:@"%@个",self.myFocusData[2]];
        }else if (indexPath.row == 2){
            cell.focusContentLabel.text = [NSString stringWithFormat:@"%@个",self.myFocusData[4]];
        }else if (indexPath.row == 3){
            cell.focusContentLabel.text = [NSString stringWithFormat:@"%@个",self.myFocusData[6]];
        }else if (indexPath.row == 4){
            cell.focusContentLabel.text = [NSString stringWithFormat:@"%@个",self.myFocusData[3]];
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            NSString *str = [self.myFocusData objectAtIndex:0];
            if ([str intValue] == 0) {
            }else{
                CirclePostsDetailsViewController *circlePostVC = [[CirclePostsDetailsViewController alloc] init];
                circlePostVC.isFromMyFoucus = YES;
                [self.navigationController pushViewController:circlePostVC animated:YES];
            }
        }else{
             NSString *str = [self.myFocusData objectAtIndex:5];
            if ([str intValue] == 0) {
            }else{
            MyNewsViewController *myNewsVC = [[MyNewsViewController alloc]init];
            [self.navigationController pushViewController:myNewsVC animated:YES];
            }
        }
    }else{
        if (indexPath.row == 0) {
             NSString *str = [self.myFocusData objectAtIndex:1];
            if ([str intValue] == 0) {
            }else{
            IllnessViewController *illnessVC = [[IllnessViewController alloc] init];
            illnessVC.isFromMyFoucus = YES;
            [self.navigationController pushViewController:illnessVC animated:YES];
            }
        }else if (indexPath.row == 1){
            //  医院
            NSString *str = [self.myFocusData objectAtIndex:2];
            if ([str intValue] == 0) {
            }else{
            HospitalViewController *hospitalVC = [[HospitalViewController alloc] init];
            hospitalVC.isFromMyFoucus = YES;
            [self.navigationController pushViewController:hospitalVC animated:YES];
            }
        }else if (indexPath.row == 2){
            // 药品
            NSString *str = [self.myFocusData objectAtIndex:4];
            if ([str intValue] == 0) {
            }else{
//            DrugViewController *drugVC = [[DrugViewController alloc] init];
//            drugVC.isFromMyFoucus = YES;
                DrugAttentionViewController *drugAttVC = [[DrugAttentionViewController alloc] init];
            [self.navigationController pushViewController:drugAttVC animated:YES];
            }
        }else if (indexPath.row == 3){
            // 急诊
            NSString *str = [self.myFocusData objectAtIndex:6];
            if ([str intValue] == 0) {
            }else{
            EmergencyViewController *emergencyVC = [[EmergencyViewController alloc] init];
            [self.navigationController pushViewController:emergencyVC animated:YES];
            }
        }else if (indexPath.row == 4){
            // 医生
            NSString *str = [self.myFocusData objectAtIndex:3];
            if ([str intValue] == 0) {
            }else{
            DoctorViewController *doctorVC = [[DoctorViewController alloc] init];
            doctorVC.isFromMyFoucus = YES;
            [self.navigationController pushViewController:doctorVC animated:YES];
            }
        }
    }
}


#pragma mark - 网路数据请求 -
- (void)_parsePersonalDataSource:(NSString *)urlString{
    [self showActionView];
    // 获取url地址
    NSString * postURL = [NSString stringWithFormat:@"%@collection/",urlString];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{@"platform": kPlatform,@"version":kVersion};
    [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        [self hiddenActionView];
        if ([statusString isEqualToString:@"0"]) {
            alertContent([responseObject objectForKey:@"msg"]);
        }else{
            NSArray * dataArr = [responseObject objectForKey:@"data"];
            for (NSDictionary *dic in dataArr) {
                MyFocusObjc *objc = [[MyFocusObjc alloc] init];
                [objc setValuesForKeysWithDictionary:dic];
                [self.myFocusData addObject:objc.count];
            }
        }
        [self.myFoucsTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];
    }];
    
}



#pragma mark - 数据懒加载 -
- (NSMutableArray *)myFocusData{
    if (_myFocusData == nil) {
        self.myFocusData = [NSMutableArray array];
    }
    return _myFocusData;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
